/**
 * jims
 */
package com.jims.master.modules.drug.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.drug.dao.*;
import com.jims.master.modules.drug.entity.*;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 药品盘点Service
 * @author zhuq
 * @version 2017-03-28
 */
@Service
@Transactional(readOnly = true)
public class DrugInventoryCheckService extends CrudService<DrugInventoryCheckDao, DrugInventoryCheck> {

    @Autowired
    private DrugStockDao drugStockDao;

    @Autowired
    private DrugImportMasterService drugImportMasterService;

    @Autowired
    private DrugImportDetailService drugImportDetailService;

    @Autowired
    private DrugImportDetailDao drugImportDetailDao;

    @Autowired
    private DrugImportMasterDao drugImportMasterDao;

    @Autowired
    private DrugExportMasterDao drugExportMasterDao;

    @Autowired
    private DrugExportService drugExportService;

    @Autowired
    private DrugExportDetailDao drugExportDetailDao;

	public DrugInventoryCheck get(String id) {
		return super.get(id);
	}
	
	public List<DrugInventoryCheck> findList(DrugInventoryCheck drugInventoryCheck) {
		return super.findList(drugInventoryCheck);
	}

    /**
     * 根据条件查询当月药品盘点盈亏统计
     * @param page
     * @param drugInventoryCheck
     * @return
     * @author CTQ
     */
	public Page<DrugInventoryCheck> findProfigLossPage(Page<DrugInventoryCheck> page, DrugInventoryCheck drugInventoryCheck) {
	    List<DrugInventoryCheck> list = dao.findListProfitAndLoss(drugInventoryCheck);
        page.setCount(list.size());
        page.setList(list);
		return page;
	}
	
	@Transactional(readOnly = false)
	public int save(DrugInventoryCheck drugInventoryCheck) {
		return super.save(drugInventoryCheck);
	}
	
	@Transactional(readOnly = false)
	public int delete(DrugInventoryCheck drugInventoryCheck) {
		return super.delete(drugInventoryCheck);
	}

    /*
    * 药品盘点，将一个药品不同批次的盘点数据保存
    * ZhuQ
    * */
    @Transactional(readOnly = false)
    public int saveList(List<DrugInventoryCheck> drugInventoryCheckList) {
        //组装盘点入库记录与盘点出库记录数据
        List<DrugStock>drugStockOutList=new ArrayList<DrugStock>();
        List<DrugStock>drugStockInList=new ArrayList<DrugStock>();
        int num=1;
        Date inventoryDate =new Date();
        for(int i=0;i<drugInventoryCheckList.size();i++){
            //实盘数大于库存数，组成盘点入库数据
            DrugStock drugStock =new DrugStock();
            drugStock =drugStockDao.get(drugInventoryCheckList.get(i).getStockId());//根据库存表id获取库存表数据
            if(drugInventoryCheckList.get(i).getActualQuantity()>drugInventoryCheckList.get(i).getMinQuantity()){
                drugStock.setQuantity(drugInventoryCheckList.get(i).getActualQuantity());
                num=num*drugStockDao.update(drugStock);
                drugStock.setQuantity(drugInventoryCheckList.get(i).getActualQuantity()-drugInventoryCheckList.get(i).getMinQuantity());
                drugStockInList.add(drugStock);
            //实盘数大于库存数，组成盘点出库数据
            }else if(drugInventoryCheckList.get(i).getActualQuantity()<drugInventoryCheckList.get(i).getMinQuantity()){
                drugStock.setQuantity(drugInventoryCheckList.get(i).getActualQuantity());
                drugStock.preUpdate();
                num=num*drugStockDao.update(drugStock);
                drugStock.setQuantity(drugInventoryCheckList.get(i).getMinQuantity()-drugInventoryCheckList.get(i).getActualQuantity());
                drugStockOutList.add(drugStock);
            }
            //给drugInventoryCheck 组装数据
            DrugInventoryCheck drugInventoryCheck=new DrugInventoryCheck();
            drugInventoryCheck=drugInventoryCheckList.get(i);
            drugInventoryCheck.updateOrgId();

            drugInventoryCheck.setStockId(drugStock.getId());
            drugInventoryCheck.setDrugCode(drugStock.getDrugCode());
            drugInventoryCheck.setInfoId(drugStock.getInfoId());
            drugInventoryCheck.setStorageId(drugStock.getStorageId());
            drugInventoryCheck.setPackageNum(Integer.valueOf(drugStock.getPackageNum()));
            drugInventoryCheck.setPackageSpec(drugStock.getPackageSpec());
            drugInventoryCheck.setPackageUnits(drugStock.getPackageUnits());
            drugInventoryCheck.setPurchasePrice(drugStock.getPurchasePrice());
            drugInventoryCheck.setBatchNo(drugStock.getBatchNo());
            drugInventoryCheck.setExpireDate(drugStock.getExpireDate());
            drugInventoryCheck.preInsert();
            drugInventoryCheck.setCreateDate(inventoryDate);
            drugInventoryCheck.setUpdateDate(inventoryDate);
            num=num*dao.insert(drugInventoryCheck);
        }

        if(drugStockInList.size()>0){
            //组成盘点入库数据
            DrugImportMaster drugImportMaster=new DrugImportMaster();
            System.out.println(UserUtils.getUser().getOrgDept().getId());
            drugImportMaster.setStorageId(UserUtils.getUser().getOrgDept().getId());// 入库库房
            drugImportMaster.setImportDate(new Date());		                    // 入库日期
            drugImportMaster.setSupplier(UserUtils.getUser().getOrgDept().getId());	// 供货方
            drugImportMaster.setImportClass("盘点");		// 入库类别
            drugImportMaster.setAccountReceivable(0.0);
            drugImportMaster.updateOrgId();
            drugImportMaster.setDocumentNo(drugImportMasterService.createDocumentNo(drugImportMaster));
            drugImportMaster.preInsert();
            num=num*drugImportMasterDao.insert(drugImportMaster);
            for(int j=0;j<drugStockInList.size();j++){
                //组装drugImportDetail数据并保存
                DrugImportDetail drugImportDetail=new DrugImportDetail();
                drugImportDetail=drugImportDetailService.createData(drugStockInList.get(j),j,drugImportMaster.getId());
                drugImportDetail.setQuantity(String.valueOf(drugStockInList.get(j).getQuantity()));
                num=num*drugImportDetailDao.insert(drugImportDetail);
            }
        }

        if(drugStockOutList.size()>0){
            //组成盘点出库数据
            DrugExportMaster drugExportMaster=new DrugExportMaster();
            System.out.println(UserUtils.getUser().getOrgDept().getId());
            drugExportMaster.setStorageId(UserUtils.getUser().getOrgDept().getId());// 入库库房
            drugExportMaster.setExportDate(new Date());		                    // 入库日期
            drugExportMaster.setReceiver(UserUtils.getUser().getOrgDept().getId());	// 供货方
            drugExportMaster.setExportClass("盘点");		// 入库类别
            drugExportMaster.setImportDate(new Date());
            drugExportMaster.setAccountReceivable(0.0);
            drugExportMaster.updateOrgId();
            drugExportMaster.setDocumentNo(drugExportService.createDocumentNo(drugExportMaster));
            drugExportMaster.preInsert();
            num=num*drugExportMasterDao.insert(drugExportMaster);
            for(int j=0;j<drugStockOutList.size();j++){
                //组装drugImportDetail数据并保存
                DrugExportDetail drugExportDetail=new DrugExportDetail();
                drugExportDetail=drugExportService.createData(drugStockOutList.get(j),j,drugExportMaster.getId());
                drugExportDetail.setInventory(Double.valueOf(drugStockOutList.get(j).getQuantity()/Double.valueOf(drugStockOutList.get(j).getPackageNum())));
                drugExportDetail.setQuantity(drugStockOutList.get(j).getQuantity());
                num=num*drugExportDetailDao.insert(drugExportDetail);
            }
        }
        return num;
    }

}